﻿using System.Windows;
using System.Windows.Controls;
using JasonPopupDemo.Data;
using System.Diagnostics;
using JsonTest1.Configure;

namespace JsonTest1.UI
{
    public class MultiTemplateListBox : ListBox
    {
        public static readonly DependencyProperty TemplateSelectorProperty = DependencyProperty.Register("TemplateSelector", typeof(DataTemplateSelector), typeof(MultiTemplateListBox), new PropertyMetadata(new PropertyChangedCallback(OnTemplateChanged)));

        public DataTemplateSelector ItemTemplateSelector
        {
            get { return (DataTemplateSelector)this.GetValue(TemplateSelectorProperty); }
            set { this.SetValue(TemplateSelectorProperty, value); }
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            ListBoxItem listBoxItem = element as ListBoxItem;

            if (listBoxItem != null)
            {
                listBoxItem.ContentTemplate = this.ItemTemplateSelector.SelectTemplate(item, this);
            }
        }

        private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }

    public class DataTemplateSelector
    {
        public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            return null;
        }
    }
    public class MainItemTemplateSelector : DataTemplateSelector
    {
        public DataTemplate HeadTemplate { get; set; }
        public DataTemplate ContentItemTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {



           
            if (item != null)
            {
                MainItem data = item as MainItem;
                if (data.Type == Constants.TYPE_HEAD_BROSER || data.Type == Constants.TYPE_HEAD_LIBRARY)
                {
                    Debug.WriteLine("----------------------------1" + HeadTemplate.ToString());
                    
                    return HeadTemplate;
                }
                else
                {
                    return ContentItemTemplate;
                }
            }

            return null;
        }
    }
}
